package org.util.core;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/5/19.
 */
public class PropertiesData {
    public static Map<String, String> map = null;
    public static final String filename = "/properties/data-resource.properties";
    public static String getProperties(String string){
        Map<String,String> map = null;
        try {
            map = new PropertiesData().getFile();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return map.get(string)==null?null:map.get(string).trim();
    }

    public Map<String,String> getFile() throws IOException {
        if(map==null){
            map = new HashMap<String, String>();
            List<String> list = FileUtils.readLines(new File(getClassDirectory() + filename));
            for (String s : list) {
                if(!s.startsWith("#")) {
                    if (s.contains("=")) {
                        String[] str = s.split("=");
                        if(str.length==1){
                            map.put(str[0].trim(), null);
                        }else{
                            map.put(str[0].trim(), str[1].trim());
                        }

                    }
                }

            }
        }
        return map;

    }
    public String getClassDirectory() throws UnsupportedEncodingException {

        URL url = Thread.currentThread().getContextClassLoader().getResource("");
        String path = URLDecoder.decode(url.getPath(), "UTF-8");
        return path;
    }
}
